package com.spring.Service;

import com.spring.Entity.AdminEntity;
import com.spring.Repository.AdminRepository;
import com.spring.Service.Interface.AdminService;
import com.spring.Util.JavaMail;
import com.spring.Util.MD5Util;
import com.spring.Util.STATUS;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by CMCC on 2016/8/26.
 */
@Service("adminService")
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminRepository adminRepository;

    @Override
    public String login(String userName, String password) {
        AdminEntity adminEntity = findByAdminName(userName);
        if (adminEntity == null) {
            return "用户不存在";
        }
        if (!adminEntity.getAdminPassword().equals(password)) {
            return "密码错误";
        } else {
            return null;
        }
    }

    @Override
    public int addAdmin(AdminEntity adminEntity) {
        adminEntity.setAdminPassword(MD5Util.calc(adminEntity.getAdminPassword()));
        adminRepository.save(adminEntity);
        return STATUS.SUCCESS;
    }

    @Override
    public int updateAdmin(AdminEntity adminEntity, String adminName) {
        int status = 1;
        if (!StringUtils.isBlank(adminName)) {
            if (JavaMail.checkMailAddress(adminName)) {
                adminEntity.setAdminName(adminName);
                status = STATUS.SUCCESS;
            } else {
                status = STATUS.FORMAT_ERROR;
            }
        }
        adminRepository.saveAndFlush(adminEntity);
        return status;
    }

    @Override
    public AdminEntity findById(String adminId) {
        if (StringUtils.isBlank(adminId)) {
            return null;
        } else {
            return adminRepository.findOne(adminId);
        }
    }

    @Override
    public AdminEntity findByAdminName(String adminName) {
        if (StringUtils.isBlank(adminName)) {
            return null;
        } else {
            return adminRepository.findOneByUserName(adminName);
        }
    }

    @Override
    public int changePassword(String adminId, String oldPassword, String newPassword) {
        Map<String, Object> response = new HashMap<>();
        oldPassword = MD5Util.calc(oldPassword);
        newPassword = MD5Util.calc(newPassword);
        AdminEntity adminEntity = findById(adminId);

        if (!adminEntity.getAdminPassword().equals(oldPassword)) {
            return STATUS.FAIL;
        } else {
            adminEntity.setAdminPassword(newPassword);
            adminRepository.saveAndFlush(adminEntity);
            return STATUS.SUCCESS;
        }
    }

    //检查管理员是否存在，存在返回false，不存在返回true
    @Override
    public Boolean checkAdmin(AdminEntity adminEntity) {
        AdminEntity check = null;
        if (!StringUtils.isBlank(adminEntity.getAdminID())) {
            check = adminRepository.findOne(adminEntity.getAdminID());
        } else if (!StringUtils.isBlank(adminEntity.getAdminName())) {
            check = adminRepository.findOneByUserName(adminEntity.getAdminName());
        }
        if (check == null) {
            return true;
        } else
            return false;
    }
}